package cn.wolfcode.rbac.sercurity.domian;

import cn.wolfcode.rbac.common.Constants;
import cn.wolfcode.rbac.domain.Employee;
import cn.wolfcode.rbac.mapper.PermissionMapper;
import cn.wolfcode.rbac.utils.JsonUtils;
import cn.wolfcode.rbac.utils.RedisUtils;
import cn.wolfcode.rbac.utils.SpringUtils;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.util.StringUtils;

import java.util.Collection;
import java.util.Set;
import java.util.stream.Collectors;

@Data
@AllArgsConstructor
public class LoginUser implements UserDetails {
    private Employee employee;
    private String uuid;

    public LoginUser(Employee employee) {
        this.employee = employee;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        // 从容器中去获取 PermissionMapper
        PermissionMapper permissionMapper = SpringUtils.getBean(PermissionMapper.class);
        RedisUtils redisUtils = SpringUtils.getBean(RedisUtils.class);
        if(employee.isAdmin()){
            String allExpression = redisUtils.get(Constants.All_EXPRESSION);
            Set<String> expressions;
            if(!StringUtils.isEmpty(allExpression)) {
                expressions = (Set<String>) JsonUtils.fromJson(allExpression, Set.class);
            } else {
                expressions = permissionMapper.selectAllExpression();
                redisUtils.set(Constants.All_EXPRESSION, JsonUtils.toJsonString(expressions));
            }
            return expressions.stream().map(expression -> {
                if(!StringUtils.isEmpty(expression)){
                    GrantedAuthority grantedAuthority = new SimpleGrantedAuthority(expression);
                    return grantedAuthority;
                }
                return new SimpleGrantedAuthority("expression");
            }).collect(Collectors.toSet());
        } else {
            Set<String> expressions;
            if(uuid == null) {
                expressions = permissionMapper.selectExpressionByEmpId(employee.getId());
                //redisUtils.set(Constants.LOGIN_EXPRESSION + uuid,JsonUtils.toJsonString(expressions));
            } else {
                String expressionsJson = redisUtils.get(Constants.LOGIN_EXPRESSION + uuid);
                expressions = (Set<String>) JsonUtils.fromJson(expressionsJson, Set.class);
            }
            return expressions.stream().map(expression->{
                if (!StringUtils.isEmpty(expression)) {
                    return new SimpleGrantedAuthority(expression);
                }
                return new SimpleGrantedAuthority("expression");
            }).collect(Collectors.toSet());
        }
    }

    @Override
    public String getPassword() {
        return employee.getPassword();
    }

    @Override
    public String getUsername() {
        return  employee.getName();
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}
